package main

import (
	"fmt"
	"strings"

	logger "gitee.com/sting/ksync/common"
	"go.uber.org/zap"
	kingpin "gopkg.in/alecthomas/kingpin.v2"
)

var (
	brokerList    = kingpin.Arg("brokerList", "brokers IP端口列表").Required().String()
	topic         = kingpin.Flag("topic", "Topic 名称").Short('t').Default("shopguide_db").String()
	consumerGroup = kingpin.Flag("cg", "kafka 消费组名称").Short('g').Default("test.cockroachdb").String()
	offsetOld     = kingpin.Flag("offsetOld", "是否从头消费消息").Short('o').Bool()
	verbose       = kingpin.Flag("verbose", "是否打印debug消息").Short('v').Bool()

	dbURL = kingpin.Arg("dburl", "database 连接地址\n eg: postgresql://kafka@127.0.0.1:26257/kafka?sslmode=disable").String()

	Log *zap.SugaredLogger
)

func main() {
	kingpin.Version("0.0.1")
	kingpin.Parse()
	brokers := strings.Split(*brokerList, ",")

	if *verbose {
		Log = logger.InitAndSetLogLevel("debug")
	} else {
		Log = logger.InitAndSetLogLevel("info")
	}

	Log.Infow("start syncing...",
		// Structured context as loosely typed key-value pairs.
		"url", brokers,
		"topic", topic,
		"consumerGroup", consumerGroup,
		"dbURL", dbURL,
		"verbose", verbose,
	)
	if len(brokers) > 0 {
		Sync(brokers, *topic, *consumerGroup, *dbURL, *offsetOld)
	}

	fmt.Println("stoped")
}
